package com.requestfactory.wire.spi;

import java.io.Serializable;

import javax.crypto.Cipher;


public final class WireSPIServerId implements Serializable {

	private static final long serialVersionUID = 20080914235712L;
	
	protected final int port;
	protected final String context;
	protected final Cipher cipher;
	
	private final String toString;
	
	public WireSPIServerId(int port, String context, Cipher cipher) { 
		assert port > -1;
		assert context != null;
		assert cipher != null;
		this.port = port;
		this.context = context.length() > 0 ? 
			(context.startsWith("/") ? context : ("/" + context)) : "/";
		this.cipher = cipher;
		this.toString = ":" + Integer.toString(this.port) + context; 
	}
	
	public int getPort() {
		return port;
	}

	public String getContext() {
		return context;
	}

	public Cipher getCipher() {
		return cipher;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj instanceof WireSPIServerId) {
			WireSPIServerId other = (WireSPIServerId)obj;
			return this.toString.equals(other.toString);
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.toString.hashCode();
	}

	@Override
	public String toString() {
		return this.toString;
	}

}
